package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserRoleGroupDao;
import com.wei.czz.framework.admin.entity.UserRoleGroupEntity;
import com.wei.czz.framework.admin.service.UserRoleGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-30 21:10:07
 * className: UserRoleGroupServiceImpl 用户与角色授权组关联服务接口类
 * version: 1.0
 * description:
 */
@Service("userRoleGroupService")
public class UserRoleGroupServiceImpl extends ServiceImpl<UserRoleGroupDao, UserRoleGroupEntity>
        implements UserRoleGroupService {

    private static final Logger log = LoggerFactory.getLogger(UserRoleGroupServiceImpl.class);

    @Transactional
    @Override
    public void batchSave(List<UserRoleGroupEntity> userRoleGroupList) {
        boolean bool = this.saveBatch(userRoleGroupList);
        log.info("批量保存角色授权组关联用户完成。bool={}", bool);
    }

    @Override
    public List<UserRoleGroupEntity> findList(List<Long> spaceIdList, List<Long> userIdList, Integer deleteStatus) {
        if (CollectionUtils.isEmpty(spaceIdList) && CollectionUtils.isEmpty(userIdList)) {
            log.info("获取角色授权组关联用户操作，传入的角色授权组主键列表和用户主键列表均为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<UserRoleGroupEntity> userRoleGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleGroupLambdaQueryWrapper.in(!CollectionUtils.isEmpty(spaceIdList), UserRoleGroupEntity::getSpaceId, spaceIdList)
                .in(!CollectionUtils.isEmpty(userIdList), UserRoleGroupEntity::getUserId, userIdList)
                .eq(Objects.nonNull(deleteStatus), UserRoleGroupEntity::getDeleteStatus, deleteStatus);
        // 查询
        List<UserRoleGroupEntity> userRoleGroupList = baseMapper.selectList(userRoleGroupLambdaQueryWrapper);
        log.info("查询角色授权组关联用户完成。spaceIdList={} userIdList={} deleteStatus={} list.size={}", spaceIdList, userIdList,
                deleteStatus, userRoleGroupList.size());
        return userRoleGroupList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<UserRoleGroupEntity> userRoleGroupList) {
        boolean bool = this.updateBatchById(userRoleGroupList);
        log.info("批量修改角色授权组关联用户操作完成。bool={}", bool);
    }

    @Override
    public void delete(List<Long> spaceIdList, List<Long> userIdList) {
        if (CollectionUtils.isEmpty(spaceIdList) && CollectionUtils.isEmpty(userIdList)) {
            log.info("删除角色授权组与用户关联数据，传入角色授权组主键列表和用户主键列表均为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<UserRoleGroupEntity> userRoleGroupLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userRoleGroupLambdaUpdateWrapper.set(UserRoleGroupEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(UserRoleGroupEntity::getUpdateTime, date)
                .set(UserRoleGroupEntity::getUpdateUser, userPo.getUsername())
                .set(UserRoleGroupEntity::getUpdateUserId, userPo.getUserId())
                .in(!CollectionUtils.isEmpty(spaceIdList), UserRoleGroupEntity::getSpaceId, spaceIdList)
                .in(!CollectionUtils.isEmpty(userIdList), UserRoleGroupEntity::getUserId, userIdList)
                .eq(UserRoleGroupEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(userRoleGroupLambdaUpdateWrapper);
        log.info("删除角色授权组与用户关联数据完成。spaceIdList={} userIdList={} count={}", spaceIdList, userIdList, count);
    }
}